home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / examples / exam21 / main.c < prev    next >
C/C++ Source or Header  |  1996-08-10  |  3KB  |  190 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6. /*
  7.  *
  8.  *    This source code is CONFIDENTIAL and
  9.  *    PROPRIETARY to Algorithms Corporation. Unauthorized
  10.  *    distribution, adaptation or use    may
  11.  *    be subject to civil and    criminal penalties.
  12.  *
  13.  *    Copyright (c) 1993 Algorithms Corporation
  14.  *    3020 Liberty Hills Drive
  15.  *    Franklin, TN  37064
  16.  *
  17.  *    ALL RIGHTS RESERVED.
  18.  *
  19.  *
  20.  *
  21.  */
  22.  
  23.  
  24.  
  25. #include "generics.h"
  26.  
  27.  
  28. static    int    cmpfun(object a, object b)
  29. {
  30.     int    av = gShortValue(a);
  31.     int    bv = gShortValue(b);
  32.     if (av < bv)
  33.         return -1;
  34.     else if (av == bv)
  35.         return 0;
  36.     else
  37.         return 1;
  38. }
  39.  
  40. #define    ADD(n)    gAddValue(t, gNewWithInt(ShortInteger, n), gNewWithStr(String, #n));
  41.  
  42. find(object bt, int n)
  43. {
  44.     object    t = gNewWithInt(ShortInteger, n);
  45.     object    f = gFind(bt, t);
  46.     gDispose(t);
  47.     if (f)
  48.         printf("found %d as \"%s\"\n", n, gStringValue(f));
  49.     else
  50.         printf("%d not found\n", n);
  51. }
  52.  
  53. ge(object bt, int n)
  54. {
  55.     object    t = gNewWithInt(ShortInteger, n), t2;
  56.     object    f = gFindGE(bt, t, &t2);
  57.     gDispose(t);
  58.     if (f)
  59.         printf("found %d as %d, \"%s\"\n", n, (int) gShortValue(t2), gStringValue(f));
  60.     else
  61.         printf("%d not found\n", n);
  62. }
  63.  
  64. gt(object bt, int n)
  65. {
  66.     object    t = gNewWithInt(ShortInteger, n), t2;
  67.     object    f = gFindGT(bt, t, &t2);
  68.     gDispose(t);
  69.     if (f)
  70.         printf("found %d as %d, \"%s\"\n", n, (int) gShortValue(t2), gStringValue(f));
  71.     else
  72.         printf("%d not found\n", n);
  73. }
  74.  
  75. le(object bt, int n)
  76. {
  77.     object    t = gNewWithInt(ShortInteger, n), t2;
  78.     object    f = gFindLE(bt, t, &t2);
  79.     gDispose(t);
  80.     if (f)
  81.         printf("found %d as %d, \"%s\"\n", n, (int) gShortValue(t2), gStringValue(f));
  82.     else
  83.         printf("%d not found\n", n);
  84. }
  85.  
  86. lt(object bt, int n)
  87. {
  88.     object    t = gNewWithInt(ShortInteger, n), t2;
  89.     object    f = gFindLT(bt, t, &t2);
  90.     gDispose(t);
  91.     if (f)
  92.         printf("found %d as %d, \"%s\"\n", n, (int) gShortValue(t2), gStringValue(f));
  93.     else
  94.         printf("%d not found\n", n);
  95. }
  96.  
  97. first(object bt)
  98. {
  99.     object    key;
  100.     object    f = gFindFirst(bt, &key);
  101.     if (f)
  102.         printf("found %d, \"%s\"\n", (int) gShortValue(key), gStringValue(f));
  103.     else
  104.         printf("not found\n");
  105. }
  106.  
  107. last(object bt)
  108. {
  109.     object    key;
  110.     object    f = gFindLast(bt, &key);
  111.     if (f)
  112.         printf("found %d, \"%s\"\n", (int) gShortValue(key), gStringValue(f));
  113.     else
  114.         printf("not found\n");
  115. }
  116.  
  117. dispose(object bt, int n)
  118. {
  119.     object    t = gNewWithInt(ShortInteger, n);
  120.     object    f = gDeepDisposeObj(bt, t);
  121.     gDispose(t);
  122.     if (f)
  123.         printf("disposed %d\n", n);
  124.     else
  125.         printf("didn't dispose %d\n", n);
  126. }
  127.  
  128.  
  129. main(int argc, char *argv[])
  130. {
  131.     object    t, k, d;
  132.  
  133.     InitDynace(&argc);
  134.  
  135.     t = gNewBTree(BTree, cmpfun);
  136.  
  137.     ADD(10);
  138.     ADD(20);
  139.     ADD(30);
  140.     ADD(40);
  141.     ADD(50);
  142.     ADD(60);
  143.     ADD(70);
  144.     ADD(80);
  145.     ADD(90);
  146.     ADD(100);
  147.  
  148.     for (k=NULL ; d = gFindPrev(t, &k) ; )
  149.         printf("found %d, \"%s\"\n", (int) gShortValue(k), gStringValue(d));
  150.  
  151. #if 0
  152.  
  153. //    gPrint(t, stdoutStream);
  154.  
  155. //    first(t);
  156. //    last(t);
  157.  
  158. //    find(t, 39);
  159.  
  160.     gPrint(t, stdoutStream);
  161. #endif
  162.  
  163.     return 0;
  164. }
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. /*
  173.  *
  174.  *    This source code is CONFIDENTIAL and
  175.  *    PROPRIETARY to Algorithms Corporation. Unauthorized
  176.  *    distribution, adaptation or use    may
  177.  *    be subject to civil and    criminal penalties.
  178.  *
  179.  *    Copyright (c) 1993 Algorithms Corporation
  180.  *    3020 Liberty Hills Drive
  181.  *    Franklin, TN  37064
  182.  *
  183.  *    ALL RIGHTS RESERVED.
  184.  *
  185.  *
  186.  *
  187.  */
  188.  
  189.  
  190.